/**
 * 食物相关API
 */

import type { Food } from '@/types'

// 使用转换后的食物数据（简化版本，只包含营养计算需要的字段）
const mockFoods: Partial<Food>[] = [
  {
    id: 1,
    name_zh: '鸡蛋',
    name_en: 'Egg',
    energy_kcal: 144,
    energy_rich: false,
    protein_g: 13.3,
    protein_rich: false,
    fat_g: 8.8,
    fat_rich: false,
    carb_g: 2.8,
    carb_rich: false,
    fiber_g: 0,
    fiber_rich: false,
    sodium_mg: 131,
    sodium_rich: false,
    calcium_mg: 56,
    calcium_rich: false,
    iron_mg: 2,
    iron_rich: false,
    vitamin_c_mg: 0,
    vitamin_c_rich: false,
    is_system: true,
    created_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z'
  },
  {
    id: 2,
    name_zh: '牛肉',
    name_en: 'Beef',
    energy_kcal: 125,
    energy_rich: false,
    protein_g: 20.2,
    protein_rich: true,
    fat_g: 4.2,
    fat_rich: false,
    carb_g: 2,
    carb_rich: false,
    fiber_g: 0,
    fiber_rich: false,
    sodium_mg: 53,
    sodium_rich: false,
    calcium_mg: 9,
    calcium_rich: false,
    iron_mg: 2.8,
    iron_rich: false,
    vitamin_c_mg: 0,
    vitamin_c_rich: false,
    is_system: true,
    created_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z'
  },
  {
    id: 3,
    name_zh: '苹果',
    name_en: 'Apple',
    energy_kcal: 54,
    energy_rich: false,
    protein_g: 0.2,
    protein_rich: false,
    fat_g: 0.2,
    fat_rich: false,
    carb_g: 13.7,
    carb_rich: false,
    fiber_g: 1.2,
    fiber_rich: false,
    sodium_mg: 1,
    sodium_rich: false,
    calcium_mg: 4,
    calcium_rich: false,
    iron_mg: 0.6,
    iron_rich: false,
    vitamin_c_mg: 4,
    vitamin_c_rich: false,
    is_system: true,
    created_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z'
  },
  {
    id: 4,
    name_zh: '西兰花',
    name_en: 'Broccoli',
    energy_kcal: 33,
    energy_rich: false,
    protein_g: 4.1,
    protein_rich: false,
    fat_g: 0.6,
    fat_rich: false,
    carb_g: 4.3,
    carb_rich: false,
    fiber_g: 1.6,
    fiber_rich: false,
    sodium_mg: 18,
    sodium_rich: false,
    calcium_mg: 67,
    calcium_rich: false,
    iron_mg: 1,
    iron_rich: false,
    vitamin_c_mg: 51,
    vitamin_c_rich: true,
    is_system: true,
    created_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z'
  },
  {
    id: 5,
    name_zh: '大米',
    name_en: 'Rice',
    energy_kcal: 391,
    energy_rich: true,
    protein_g: 8.3,
    protein_rich: false,
    fat_g: 0.8,
    fat_rich: false,
    carb_g: 86.4,
    carb_rich: true,
    fiber_g: 0.7,
    fiber_rich: false,
    sodium_mg: 5,
    sodium_rich: false,
    calcium_mg: 13,
    calcium_rich: false,
    iron_mg: 2.3,
    iron_rich: false,
    vitamin_c_mg: 0,
    vitamin_c_rich: false,
    is_system: true,
    created_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z'
  }
]

/**
 * 获取所有食物列表
 */
export async function getFoods(): Promise<Food[]> {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 100))

  return mockFoods as Food[]
}

/**
 * 根据ID获取食物详情
 */
export async function getFoodById(id: number): Promise<Food | null> {
  await new Promise(resolve => setTimeout(resolve, 50))

  return (mockFoods.find(food => food.id === id) as Food) || null
}

/**
 * 搜索食物
 */
export async function searchFoods(query: string): Promise<Food[]> {
  await new Promise(resolve => setTimeout(resolve, 100))

  if (!query.trim()) {
    return mockFoods as Food[]
  }

  const lowerQuery = query.toLowerCase()
  return mockFoods.filter(food =>
    food.name_zh?.toLowerCase().includes(lowerQuery) ||
    food.name_en?.toLowerCase().includes(lowerQuery)
  ) as Food[]
}

/**
 * 获取富含特定营养素的食物
 */
export async function getFoodsRichIn(nutrient: string): Promise<Food[]> {
  await new Promise(resolve => setTimeout(resolve, 100))

  const richField = `${nutrient}_rich` as keyof Food
  return mockFoods.filter(food => food[richField] === true) as Food[]
}
